package com.ws.service.impl;

import com.github.pagehelper.Page;
import com.ws.common.CommonConstant;
import com.ws.common.PaasPage;
import com.ws.entity.OrderItem;
import com.ws.repository.OrderItemRepository;
import com.ws.service.OrderService;
import com.ws.util.PageUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author ：ws
 * @date ：Created in 2023/10/24 12:30
 * @description：TODO
 * @modified By：
 * @version: 1.0.0
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderItemRepository orderItemRepository;

    @Override
    public PaasPage<OrderItem> getOrderPage(Pageable pageable, Long bookId,String statusCd) {
        PageUtil.setPageAndOrderBy(pageable);
        OrderItem orderItem = new OrderItem();
        orderItem.setBookId(bookId);
        if(StringUtils.isEmpty(statusCd)){
            statusCd = CommonConstant.ADD_STATUS_CD;
        }
        orderItem.setStatusCd(statusCd);
        Page<OrderItem> orderItemPage = orderItemRepository.getOrderPage(orderItem);
        return PageUtil.transformPage(orderItemPage);
    }

    @Override
    public OrderItem saveOrder(OrderItem orderItem) {
        if(null == orderItem.getOrderId()){
            orderItem.setStatusCd(CommonConstant.ADD_STATUS_CD);
            orderItem.setCreateTime(new Date());
            orderItemRepository.saveOrder(orderItem);
        }else{
            orderItemRepository.updateOrder(orderItem);
            return orderItem;
        }

        return orderItem;
    }

    @Override
    public OrderItem getOrderBasicInfo(Long orderId) {
        return orderItemRepository.getOrderBasicInfo(orderId);
    }

    @Override
    public void delOrder(Long orderId) {
        orderItemRepository.delOrder(orderId);
    }

    @Override
    public OrderItem updateOrder(Long orderId, OrderItem orderItem) {
        orderItemRepository.updateOrder(orderItem);
        return orderItem;
    }
}
